package com.energy.analysis.report.controller;

import com.energy.analysis.comm.base.BaseController;
import com.energy.analysis.comm.page.Page;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.StringUtils;
import com.energy.analysis.report.entity.CollectDataDay;
import com.energy.analysis.report.service.CollectDataDayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/report")
public class ReportController extends BaseController<CollectDataDay> {

    @Autowired
    private CollectDataDayService collectDataDayService;

    @RequiresPermissions("report_day:view")
    @RequestMapping("/day")
    public ModelAndView dayIndex(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("report/report_day");
        return model;
    }

    @RequiresPermissions("report_month:view")
    @RequestMapping("/month")
    public ModelAndView monthIndex(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("report/report_month");
        return model;
    }

    @RequiresPermissions("report_quarter:view")
    @RequestMapping("/quarter")
    public ModelAndView quarterIndex(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("report/report_quarter");
        return model;
    }

    @RequiresPermissions("report_year:view")
    @RequestMapping("/year")
    public ModelAndView yearIndex(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("report/report_year");
        return model;
    }

    @RequiresPermissions("report_economic:view")
    @RequestMapping("/econ")
    public ModelAndView econIndex(HttpSession session) {
        pageInitToSession(session);
        ModelAndView model = new ModelAndView("report/report_econ");
        return model;
    }

    /**
     * 返回日报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_day:view")
    @RequestMapping("/day/list")
    @ResponseBody
    public Page<CollectDataDay> dayList(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String searchDateTime = (String) page.getParams().get("searchDateTime");
        if (StringUtils.isNotEmpty(searchDateTime)) {
            page.getParams().put("dt", new java.sql.Date(DateUtils.toDate(searchDateTime).getTime()).toString());
        }
        page.setRowsMap(collectDataDayService.getDataByDateTime(page));
        page.setTotal(collectDataDayService.getDataByDateTimeCount(page));
        return page;
    }

    /**
     * 返回日报表导出
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_day:view")
    @RequestMapping("/day/list/export")
    public void dayListExport(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String searchDateTime = (String) page.getParams().get("searchDateTime");
        if (StringUtils.isNotEmpty(searchDateTime)) {
            page.getParams().put("dt", new java.sql.Date(DateUtils.toDate(searchDateTime).getTime()).toString());
        }
        List<Map<String, Object>> retList = collectDataDayService.getDataByDateTime(page);
        String fileName = "日报表" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "report_date", fileName, searchDateTime);
    }

    /**
     * 返回月报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_month:view")
    @RequestMapping("/month/list")
    @ResponseBody
    public Page<CollectDataDay> monthList(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String searchDateTime = (String) page.getParams().get("searchDateTime");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(searchDateTime)) {
            Date date = DateUtils.toDate(searchDateTime);
            //获取最大日
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(DateUtils.getYear(date), DateUtils.getMonth(date), null, stationName);
            if (dt == null) {
                return page;
            }
            page.getParams().put("dt", dt.toString());
        }

        page.setRowsMap(collectDataDayService.getDataByDateTime(page));
        page.setTotal(collectDataDayService.getDataByDateTimeCount(page));
        return page;
    }

    /**
     * 返回月报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_month:view")
    @RequestMapping("/month/list/export")
    public void monthListExport(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String searchDateTime = (String) page.getParams().get("searchDateTime");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(searchDateTime)) {
            Date date = DateUtils.toDate(searchDateTime);
            //获取最大日
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(DateUtils.getYear(date), DateUtils.getMonth(date), null, stationName);
            if (dt == null) {
                return;
            }
            page.getParams().put("dt", dt.toString());
        }
        List<Map<String, Object>> retList = collectDataDayService.getDataByDateTime(page);
        String fileName = "月报表" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "report_date", fileName, searchDateTime);
    }

    /**
     * 返回年报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_year:view")
    @RequestMapping("/year/list")
    @ResponseBody
    public Page<CollectDataDay> yearList(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String year = (String) page.getParams().get("year");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(year)) {
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(Integer.parseInt(year), null, null, stationName);
            if (dt == null) {
                return page;
            }
            page.getParams().put("dt", dt.toString());
        }
        page.setRowsMap(collectDataDayService.getDataByDateTime(page));
        page.setTotal(collectDataDayService.getDataByDateTimeCount(page));
        return page;
    }

    /**
     * 导出年报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_year:view")
    @RequestMapping("/year/list/export")
    public void yearListExport(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String year = (String) page.getParams().get("year");
        String quarter = (String) page.getParams().get("quarter");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(year) && StringUtils.isNotEmpty(quarter)) {
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(Integer.parseInt(year), null, Integer.parseInt(quarter), stationName);
            if (dt == null) {
                return;
            }
            page.getParams().put("dt", dt.toString());
        }
        List<Map<String, Object>> retList = collectDataDayService.getDataByDateTime(page);
        String fileName = "年报表" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "report_date", fileName, year + "年");
    }

    /**
     * 返回季度报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_quarter:view")
    @RequestMapping("/quarter/list")
    @ResponseBody
    public Page<CollectDataDay> quarterList(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String year = (String) page.getParams().get("year");
        String quarter = (String) page.getParams().get("quarter");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(year) && StringUtils.isNotEmpty(quarter)) {
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(Integer.parseInt(year), null, Integer.parseInt(quarter), stationName);
            if (dt == null) {
                return page;
            }
            page.getParams().put("dt", dt.toString());
        }
        page.setRowsMap(collectDataDayService.getDataByDateTime(page));
        page.setTotal(collectDataDayService.getDataByDateTimeCount(page));
        return page;
    }

    /**
     * 导出季度报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_quarter:view")
    @RequestMapping("/quarter/list/export")
    public void quarterListExport(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String year = (String) page.getParams().get("year");
        String quarter = (String) page.getParams().get("quarter");
        String stationName = (String) page.getParams().get("stationName");
        if (StringUtils.isNotEmpty(year) && StringUtils.isNotEmpty(quarter)) {
            java.sql.Date dt = collectDataDayService.getMonthMaxDay(Integer.parseInt(year), null, Integer.parseInt(quarter), stationName);
            if (dt == null) {
                return;
            }
            page.getParams().put("dt", dt.toString());
        }
        List<Map<String, Object>> retList = collectDataDayService.getDataByDateTime(page);
        String fileName = "季度报表" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, retList, "report_date", fileName, year + "年" + quarter + "季度");
    }

    /**
     * 返经济分析报表
     *
     * @param request
     * @param response
     * @param page
     * @return
     */
    @RequiresPermissions("report_economic:view")
    @RequestMapping("/econ/list")
    @ResponseBody
    public Page<CollectDataDay> econList(HttpServletRequest request, HttpServletResponse response, Page<CollectDataDay> page) {
        initPage(page);
        String beginTime = (String) page.getParams().get("beginTime");
        String endTime = (String) page.getParams().get("endTime");
        String stationName = (String) page.getParams().get("stationName");
        java.sql.Date dt = collectDataDayService.getMonthEconMaxDay(beginTime, endTime, stationName);
        if (dt == null) {
            return page;
        }
        page.getParams().put("dt", dt.toString());
        page.setRowsMap(collectDataDayService.getDataEconByDateTime(page));
        page.setTotal(collectDataDayService.getDataEconByDateTimeCount(page));
        return page;
    }

    @RequiresPermissions("report_economic:view")
    @RequestMapping("/econ/list/export")
    public void econListExport(HttpServletRequest request, HttpServletResponse response, String beginTime, String endTime, String stationName, Page<CollectDataDay> page) {
        page.getParams().put("beginTime", beginTime);
        page.getParams().put("endTime", endTime);
        page.getParams().put("stationName", stationName);
        java.sql.Date dt = collectDataDayService.getMonthEconMaxDay(beginTime, endTime, stationName);
        if (dt == null) {
            return;
        }
        page.getParams().put("dt", dt.toString());
        page.setLimit(9999999);//因为需要下载全部页数据
        page.setOffset(0);
        List<Map<String, Object>> allList = collectDataDayService.getDataEconByDateTime(page);
        String fileName = "经济分析报表" + DateUtils.getYmdHmsDate() + ".xls";
        exportFile(response, allList, "report_econ", fileName, endTime);
    }

}
